#region '"NameSpaces"' 
using System.IO; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Collections; 
#endregion 

#region '"Provider Factory"' 
using Microsoft.VisualBasic;
using System;
using System.Data;
using System.Diagnostics;
namespace BizBloxCSTests
{
    public class TestProviderFactory  
    { 
        
        protected internal enum EnumProviders 
        { 
            SQLClient,
        } 
        
        
        private static EnumProviders _provider; 
        private static bool _IsProviderFetched; 
        //private static string _ConnString; 
        
        private TestProviderFactory() 
        { 
        } 
        
        protected internal static IDbCommand GetCommand() 
        { 
            
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    return new SqlCommand(); 
            }
            
            return null;
        } 
        
        
        protected internal static IDbCommand GetCommand( string strCmdText, CommandType cmdType ) 
        { 
            
            IDbCommand cmd = GetCommand(); 
            PrepareCommand( ref cmd, strCmdText, cmdType ); 
            return cmd; 
            
        } 
        
        
		protected internal static IDbCommand GetCommand( string strCmdText, IDbDataParameter[] ParameterArray ) 
        { 
            
            IDbCommand cmd = GetCommand(); 
            string strParams = ""; 
            int i, count; 
            count = ParameterArray.Length - 1; 
            for ( i=0; i <= count; i++ ) 
            { 
                switch ( GetProvider() ) 
                {
                    case EnumProviders.SQLClient:
                        strParams = strParams + ParameterArray[i].ParameterName + ( ParameterArray[i].Direction == ParameterDirection.Output | ParameterArray[i].Direction == ParameterDirection.InputOutput ? " Output" : "" ) + ", "; 
                        break;
                }
                
                cmd.Parameters.Add( ParameterArray[i] ); 
            } 
            cmd.CommandText = PrepareCommandText( strCmdText, strParams.Remove( strParams.Length - 2, 2 ) ); 
            cmd.CommandType = CommandType.Text; 
            
            return cmd; 
        } 
        
        
        protected internal static IDbConnection GetConnection() 
        { 
            
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    return new SqlConnection(); 
            }
            return null;            
        } 
        
        
        protected internal static IDbConnection GetConnection( string strConnString ) 
        { 
            IDbConnection con = GetConnection(); 
            con.ConnectionString = strConnString; 
            con.Open(); 
            return con; 
        } 
        
        
        protected internal static IDbDataAdapter GetAdapter() 
        { 
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    return new SqlDataAdapter(); 
            }			
			return null;  
        } 
        
        
        protected internal static IDbDataAdapter GetAdapter( string strCommandText, CommandType cmdType, string strConnString ) 
        { 
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    return new SqlDataAdapter( PrepareCommandText( strCommandText, cmdType.ToString() ), strConnString ); 
            }
			return null;            
        } 
        
        
        protected internal static IDbDataAdapter GetAdapter( string strCommandText, string strConnString ) 
        { 
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    return new SqlDataAdapter( strCommandText, strConnString ); 
            }
			return null;            
        } 
        
        
        protected internal static IDbDataParameter GetParameter() 
        { 
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    return new SqlParameter(); 
            }
            return null;
        } 
        
        
        protected internal static IDbDataParameter GetParameter( string paramName, ParameterDirection paramDirection, object paramValue, DbType paramtype ) 
        { 
            IDbDataParameter param; 
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    param = new SqlParameter( paramName, paramtype ); 
                    if ( paramDirection == ParameterDirection.Input | paramDirection == ParameterDirection.InputOutput ) 
                    { 
                        param.Value = paramValue; 
                    } 
                    param.Direction = paramDirection; 
					return param;
            }
            return null;  
        } 
        
        
        protected internal static IDbDataParameter GetParameter( string paramName, object paramValue ) 
        { 
            IDbDataParameter param; 
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    param = new SqlParameter( paramName, paramValue ); 
                    return param;
            }
            return null;             
        } 
        
        
        private static void PrepareCommand( ref IDbCommand cmd, string strSQL, CommandType cmdType ) 
        { 
            
            cmd.CommandType = CommandType.Text; 
            cmd.CommandText = strSQL; 
            
        } 
        
        
        protected internal static string PrepareCommandText( string strSQL, string strParams ) 
        {             
            switch ( GetProvider() ) 
            {
                case EnumProviders.SQLClient:
                    return "Exec " + strSQL + " " + strParams; 
            }            
            return "";
        } 
        
        
        protected internal static EnumProviders GetProvider() 
        { 
            if ( !( _IsProviderFetched ) ) 
            { 
                //             Dim Idt As IDictionary = CType(ConfigurationSettings.GetConfig("ConnectionInfo"), IDictionary)
                //            Return CType(Idt("Provider"), EnumProviders)
                _IsProviderFetched = true; 
                _provider = EnumProviders.SQLClient; 
            } 
            return _provider; 
        } 
        
    } 
    
    #endregion 
    
} 
